#include "GRO_Gui.h"
#include <iostream>
#include "SDL_image.h"
#include <unistd.h>
using namespace std;

#include "GRO_Backend_Sprig.h"
#include "GRO_Font_NFont.h"

SDL_Surface* screen;
Uint8* keystates;

#include "sprig.h"
#include "NFont.h"
#include "SDL_ttf.h"





void setbool_callback(bool* ptr, bool value)
{
    if(ptr != NULL)
        *ptr = value;
}


void start()
{
    using namespace GRO;
 
    EnableUnicode();
    KeyRepeat();
    
    
    Gui& gui = Gui::instance();
    gui.init(screen->format, screen->w, screen->h);
    
    NavCursor* nav = new NavCursor(new KeyboardSettings());
    nav->gui = &gui;
    gui.cursors.push_back(nav);
    
    
    
    //static_cast<MouseCursor*>(gui->cursors.front())->image = IMG_Load("../images/pointer.png");
    //SDL_ShowCursor(0);
    
    //SPG_PushThickness(1);
    
    NFont f1;
    TTF_Font* ttf = TTF_OpenFont("../fonts/FreeSans.ttf", 14);
    SDL_Color fgColor = {0, 0, 0, SDL_ALPHA_OPAQUE};
    f1.load(ttf, fgColor);
    f1.setDest(screen);
    
    NFont whiteFont;
    whiteFont.load(screen, NFont::verticalGradient(f1.getSurface(), 0xffffff, 0x999999, 0));
    
    GRO_Font* font1 = new GRO_Font_NFont(&f1);
    GRO_Font* font2 = new GRO_Font_NFont(&whiteFont);
    
    Window* window = new Window(30, 30, 400, 300, "My Window", font2);
    
    gui.add(window);
    
    Panel* panel = window;
    panel->setGridSize(4, 4);
    
    Widget* widget = new Text("Foods I Like:", font1);
    panel->set(0,0,widget);
    
     

    Widget* widget3 = widget = new Checkbox(20, 20, true);
    panel->set(1,0,widget);
 

 
    widget = new Text(" Pizza", font1);
    panel->set(1,1,widget);
    

    
    Widget* widget2 = widget = new Checkbox(20, 20, false);
    panel->set(2,0,widget);

    Widget* widget4 = widget = new Text("Broccoli", font1);
    panel->set(2,1,widget);

    // Editable text
    Group* group = new Group(200, 200);
    
    widget = new ScrollBar(15, group->area.h - 20, 15, 10, 15, 15, DOWN);

    widget->setDock(DOCK_UP_RIGHT);
    widget->padding.up = 10;
    group->pin(widget);
    group->setScroll(static_cast<ScrollBar*>(widget), DOWN);
    
    widget = new Text("I like this one because:\n", font1);
    dynamic_cast<Text*>(widget)->editable = 1;
    dynamic_cast<Text*>(widget)->textMax = 1000;
    widget->area.w = 170;
    widget->area.h = 400;
    
    panel->set(3, 1, group);
    group->set(0, 0, widget);
    panel->fitContents();
    
    
    
    
    NavNode* node = new NavNode(widget2);
    
    nav->currentNode = node;
    nav->currentWidget = widget2;
    
    NavNode* nextNode = new NavNode(widget3);
    
    node->up = nextNode;
    nextNode->down = node;
    
    NavNode* textNode = new NavNode(widget4);
    
    node->right = textNode;
    textNode->left = node;
    textNode->up = nextNode;
    
    
    
    
    
    
    
    
    
    
    panel = new Panel(300, 400, 200, 200);
    
    gui.add(panel);
    
    widget = new Checkbox(40, 40, true);
    
    panel->set(0,0,widget);
    
    widget = new Button(40, 40);
    
    ColorPickerSimple* colorPicker = new ColorPickerSimple(300, 200, 350, 150, font1, &gui);
    
    colorPicker->setFolded(1);
    gui.add(colorPicker);
    
    
    widget->onClick.connect<Panel, void>(colorPicker, &Panel::open);
    
    
    panel->set(1,0,widget);
    
    
    widget = new Slider(100, 60);
    static_cast<Slider*>(widget)->direction = LEFT;
    
    panel->set(2, 0, widget);
    
    widget = new Slider(60, 100);
    static_cast<Slider*>(widget)->direction = UP;
    
    panel->set(2, 1, widget);
    
    widget = new Button(100, 60, new Text("This is a\nborderless\nbutton", font1));
    
    panel->set(1, 1, widget);
    
    
    panel = Dialog(&gui, 370, 370, 200, 100, "This is\na pop-up message.", font1, "OK", font1);
    panel->setFolded(1);
    panel->grabInput = 1;
    
    widget->onClick.connect<Panel, void>(panel, &Panel::open);
    
    
    
    
    
    
    panel = new Panel(400, 5, 300, 300);
    
    group = new Group(200, 100);
    Slider* slider = new Slider(100, 40);
    Text* text = new Text("100", font1);
    int value = 0;
    text->link(&value);
    gui.add(panel);
    panel->set(0,0,group);
    group->set(0, 0, text);
    group->set(0, 1, slider);
    
    slider->link(&value);
    
    
    widget = new ScrollBar(15, group->area.h - 20, 15, 10, 15, 15, DOWN);

    widget->setDock(DOCK_UP_RIGHT);
    widget->padding.up = 10;
    group->pin(widget);
    group->setScroll(static_cast<ScrollBar*>(widget), DOWN);
    
    Group* group2 = new Group(200, 100);
    
    widget = new Spinner(50, 70);
    static_cast<Spinner*>(widget)->direction = DOWN;
    static_cast<Spinner*>(widget)->max = 4000;
    static_cast<Spinner*>(widget)->min = 351;
    
    text = new Text("4000", font1);
    text->link(&(static_cast<Spinner*>(widget)->value));
    
    group->set(1, 0, group2);
    group2->set(0,0,text);
    group2->set(0,1,widget);
    
    float bgT = 0;
    bool bgIncrease = true;
    int bgR = 255, bgG = 255, bgB = 255;
    
    colorPicker->rSlider->link(&bgR);
    colorPicker->gSlider->link(&bgG);
    colorPicker->bSlider->link(&bgB);
    
    Uint32 frametime;
    float targetFps = 60;
    float waitTime = 1000.0f / targetFps;
    
    int mx = 0, my = 0;
    
    bool done = 0;
    
    // Add QUIT shortcut
    Shortcut* shortcut = new Shortcut(KMOD_CTRL, SDLK_q);
    shortcut->onExecute.connect<void, bool*, bool>(setbool_callback, &done, true);
    gui.addShortcut(shortcut);
    
    
    SDL_Event event;
    int frameStart;
    while (!done)
    {
        frameStart = SDL_GetTicks();
        SDL_GetMouseState(&mx, &my);
        
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
                done = 1;
            
            if(gui.event(event) != NULL)
                continue;
            

            if (event.type == SDL_KEYDOWN)
            {
                
            }
        }
        
        gui.update();
        
        if(bgIncrease)
        {
            bgT += 0.05f*gui.dt;
            if(bgT > 1.0f)
            {
                bgT = 1.0f;
                bgIncrease = false;
            }
        }
        else
        {
            bgT -= 0.5f*gui.dt;
            if(bgT < 0.0f)
            {
                bgT = 0.0f;
                bgIncrease = true;
                bgR = rand()%256;
                bgG = rand()%256;
                bgB = rand()%256;
            }
        }
        SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, Uint8(bgR*bgT), Uint8(bgG*bgT), Uint8(bgB*bgT)));
        
        gui.draw();
        
        
        SDL_Flip(screen);
        
        frametime = SDL_GetTicks() - frameStart;

        if (SDL_GetTicks() - frameStart < waitTime)
            SDL_Delay((int)(waitTime - frametime));

    }
    
    
}







int main(int argc, char* argv[])
{
    
    char buff[] = "SDL_VIDEO_CENTERED=center";
    
    SDL_putenv(buff);
    
    screen = SPG_InitSDL(800, 600, 32, SDL_INIT_VIDEO, SDL_SWSURFACE);
    if(screen == NULL)
        return 0;
    
    SDL_WM_SetCaption("Starter Code", NULL);
    
    keystates = SDL_GetKeyState(NULL);
    
    TTF_Init();
    
    GRO::GRO_SetRenderer(new GRO::Renderer_Sprig(screen));
    
    srand(time(NULL));
    start();
    
    SDL_Quit();
    return 0;
}
